package com.mofofa.maozy.components.dialog

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.*
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.mofofa.maozy.ProjectConfig
import com.mofofa.maozy.R
import com.mofofa.maozy.base.BaseDialogAction
import com.mofofa.maozy.databinding.DialogMaozyAppReviewBinding
import com.mofofa.maozy.extend.openOnGooglePlayStore
import com.mofofa.maozy.extend.recordMaozyClickMaozyEvent
import com.mofofa.maozy.extend.startMaozyToMaozyNextMaozyActivity
import com.mofofa.maozy.me.FeedbackActivity
import com.mofofa.maozy.utils.AppConfigUtil
import com.mofofa.maozy.utils.analytics.util.AppReviewUtil

class AppReviewDialog : DialogFragment(), BaseDialogAction {

    //星级
    private var mRate = 0

    //显示文案
    private var mContent: String? = ""

    companion object {
        fun showMaozyDialog(
            fragmentManager: FragmentManager,
            content: String?
        ) {
            val dialog = AppReviewDialog()
            dialog.mContent = content
            dialog.showNow(fragmentManager, AppReviewDialog::class.java.simpleName)
        }
    }

    lateinit var mMaozyViewMaozyBinding: DialogMaozyAppReviewBinding

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        dialog?.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mMaozyViewMaozyBinding =
            DialogMaozyAppReviewBinding.inflate(layoutInflater)

        return mMaozyViewMaozyBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initMaozySubviews()
        dialog?.setCancelable(true)
        dialog?.setCanceledOnTouchOutside(true)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NO_TITLE, R.style.Dialog)
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onStart() {
        super.onStart()

        dialog?.window?.let {
            it.attributes.gravity = Gravity.CENTER
            it.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
//            it.setWindowAnimations(R.style.bottomDialogAnimation)
        }

        mMaozyViewMaozyBinding.textAppReviewTitle.text = mContent

    }

    override fun initMaozySubviews() {
        try {
            mMaozyViewMaozyBinding.imageAppReviewStar1.setOnClickListener {
                mMaozyViewMaozyBinding.imageAppReviewStar1.setImageResource(R.drawable.maozy_app_review_star_yes)
                mMaozyViewMaozyBinding.imageAppReviewStar2.setImageResource(R.drawable.maozy_app_review_star_no)
                mMaozyViewMaozyBinding.imageAppReviewStar3.setImageResource(R.drawable.maozy_app_review_star_no)
                mMaozyViewMaozyBinding.imageAppReviewStar4.setImageResource(R.drawable.maozy_app_review_star_no)
                mMaozyViewMaozyBinding.imageAppReviewStar5.setImageResource(R.drawable.maozy_app_review_star_no)

                mRate = 1
                mMaozyViewMaozyBinding.imageAppReviewStar1.recordMaozyClickMaozyEvent(
                    "1"
                )

                mMaozyViewMaozyBinding.textAppReviewRate.setBackgroundResource(R.drawable.maozy_app_review_active_enable)
            }

            mMaozyViewMaozyBinding.imageAppReviewStar2.setOnClickListener {
                mMaozyViewMaozyBinding.imageAppReviewStar1.setImageResource(R.drawable.maozy_app_review_star_yes)
                mMaozyViewMaozyBinding.imageAppReviewStar2.setImageResource(R.drawable.maozy_app_review_star_yes)
                mMaozyViewMaozyBinding.imageAppReviewStar3.setImageResource(R.drawable.maozy_app_review_star_no)
                mMaozyViewMaozyBinding.imageAppReviewStar4.setImageResource(R.drawable.maozy_app_review_star_no)
                mMaozyViewMaozyBinding.imageAppReviewStar5.setImageResource(R.drawable.maozy_app_review_star_no)

                mRate = 2
                mMaozyViewMaozyBinding.imageAppReviewStar2.recordMaozyClickMaozyEvent(
                    "2"
                )

                mMaozyViewMaozyBinding.textAppReviewRate.setBackgroundResource(R.drawable.maozy_app_review_active_enable)
            }

            mMaozyViewMaozyBinding.imageAppReviewStar3.setOnClickListener {
                mMaozyViewMaozyBinding.imageAppReviewStar1.setImageResource(R.drawable.maozy_app_review_star_yes)
                mMaozyViewMaozyBinding.imageAppReviewStar2.setImageResource(R.drawable.maozy_app_review_star_yes)
                mMaozyViewMaozyBinding.imageAppReviewStar3.setImageResource(R.drawable.maozy_app_review_star_yes)
                mMaozyViewMaozyBinding.imageAppReviewStar4.setImageResource(R.drawable.maozy_app_review_star_no)
                mMaozyViewMaozyBinding.imageAppReviewStar5.setImageResource(R.drawable.maozy_app_review_star_no)

                mRate = 3
                mMaozyViewMaozyBinding.imageAppReviewStar3.recordMaozyClickMaozyEvent(
                    "3"
                )
                mMaozyViewMaozyBinding.textAppReviewRate.setBackgroundResource(R.drawable.maozy_app_review_active_enable)
            }

            mMaozyViewMaozyBinding.imageAppReviewStar4.setOnClickListener {
                mMaozyViewMaozyBinding.imageAppReviewStar1.setImageResource(R.drawable.maozy_app_review_star_yes)
                mMaozyViewMaozyBinding.imageAppReviewStar2.setImageResource(R.drawable.maozy_app_review_star_yes)
                mMaozyViewMaozyBinding.imageAppReviewStar3.setImageResource(R.drawable.maozy_app_review_star_yes)
                mMaozyViewMaozyBinding.imageAppReviewStar4.setImageResource(R.drawable.maozy_app_review_star_yes)
                mMaozyViewMaozyBinding.imageAppReviewStar5.setImageResource(R.drawable.maozy_app_review_star_no)

                mRate = 4
                mMaozyViewMaozyBinding.imageAppReviewStar4.recordMaozyClickMaozyEvent(
                    "4"
                )
                mMaozyViewMaozyBinding.textAppReviewRate.setBackgroundResource(R.drawable.maozy_app_review_active_enable)
            }

            mMaozyViewMaozyBinding.imageAppReviewStar5.setOnClickListener {
                mMaozyViewMaozyBinding.imageAppReviewStar1.setImageResource(R.drawable.maozy_app_review_star_yes)
                mMaozyViewMaozyBinding.imageAppReviewStar2.setImageResource(R.drawable.maozy_app_review_star_yes)
                mMaozyViewMaozyBinding.imageAppReviewStar3.setImageResource(R.drawable.maozy_app_review_star_yes)
                mMaozyViewMaozyBinding.imageAppReviewStar4.setImageResource(R.drawable.maozy_app_review_star_yes)
                mMaozyViewMaozyBinding.imageAppReviewStar5.setImageResource(R.drawable.maozy_app_review_star_yes)

                mRate = 5
                mMaozyViewMaozyBinding.imageAppReviewStar5.recordMaozyClickMaozyEvent(
                    "5"
                )
                mMaozyViewMaozyBinding.textAppReviewRate.setBackgroundResource(R.drawable.maozy_app_review_active_enable)
            }

            mMaozyViewMaozyBinding.textDialogAppReviewNotNow.setOnClickListener {
                mMaozyViewMaozyBinding.textDialogAppReviewNotNow.recordMaozyClickMaozyEvent(
                    mRate.toString()
                )
                dismiss()
            }

            mMaozyViewMaozyBinding.textAppReviewRate.setOnClickListener {
                mMaozyViewMaozyBinding.textAppReviewRate.recordMaozyClickMaozyEvent(
                    mRate.toString()
                )

                if (mRate <= 0) {
                    Toast.makeText(
                        requireContext(),
                        "Please select star",
                        Toast.LENGTH_SHORT
                    ).show()
                    return@setOnClickListener
                }

                //如果大于等于4星，跳转系统评介
                if (mRate >= 4) {
                    try {
                        //如果配置了使用系统评介，则走
                        if (AppConfigUtil.mMaozySysMaozyConfig?.customize?.isShowSysReviewipetrpide() == true) {
                            AppReviewUtil.launchReviewFlow(requireContext() as AppCompatActivity) {
                                if (!it) {
                                    //跳转应用市场
                                    requireContext().openOnGooglePlayStore(ProjectConfig.MAOZY_Playstore_Link)
                                }
                                dismiss()
                            }
                        } else {
                            //跳转应用市场
                            requireContext().openOnGooglePlayStore(ProjectConfig.MAOZY_Playstore_Link)
                            dismiss()
                        }
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                } else {
                    //跳转反馈页
                    (requireContext() as AppCompatActivity).startMaozyToMaozyNextMaozyActivity<FeedbackActivity>()
                }
            }

            mMaozyViewMaozyBinding.imageDialogAppReviewClose.setOnClickListener {
                mMaozyViewMaozyBinding.imageDialogAppReviewClose.recordMaozyClickMaozyEvent(
                    mRate.toString()
                )
                dismiss()
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    override fun showMaozyWithMaozyUIMaozyTag(
        gTag: String,
        fragmentManager: FragmentManager
    ) {
    }

    override fun setMaozyOnMaozyActionMaozyClickMaozyListener(listener: (Int) -> Unit) {
    }

}